package com.uchiha.nova.core.event;


import com.uchiha.nova.core.feign.ILogClient;
import com.uchiha.nova.core.model.LogApi;
import com.uchiha.nova.core.props.NovaProperties;
import com.uchiha.nova.core.props.ServerInfo;
import com.uchiha.nova.core.publisher.EventConstant;
import com.uchiha.nova.core.utils.LogAbstractUtil;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 周林静
 * @Date 2021-10-25 15:12
 * @Version 1.0
 */
@Slf4j
@AllArgsConstructor
@Component
public class ApiLogListener {
    @Resource
    private ILogClient logService;

    private final ServerInfo serverInfo;

    private final NovaProperties novaProperties;


    @Async
    @Order
    @EventListener(ApiLogEvent.class)
    public void saveApiLog(ApiLogEvent event) {
        log.info("ApiLogListener-saveApiLog:{}",event);
        Map<String, Object> source = (Map<String, Object>) event.getSource();
        LogApi logApi = (LogApi) source.get(EventConstant.EVENT_LOG);
        LogAbstractUtil.addOtherInfoToLog(logApi, novaProperties, serverInfo);
        logService.saveApiLog(logApi);
    }

}
